package org.dbutils.dao.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;
import org.dbutils.dao.HandlerDao;
import org.dbutils.entity.Staff;
import org.dbutils.util.DruidUtil;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author ZQX
 * @date 2025-02-27
 */
public class HandlerDaoImpl implements HandlerDao {

    QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());

    @Override
    public Map<String, Object> selectById4Map(int id) {
        String sql = "select id,name,phone,age,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs where id=?";
        try {
            return qr.query(sql, new MapHandler(), id);
        } catch (SQLException e) {
            throw new RuntimeException("查询异常", e);
        }
    }

    @Override
    public List<Map<String, Object>> selectAll4MapList() {
        String sql = "select id,name,phone,age,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs";
        try {
            return qr.query(sql, new MapListHandler());
        } catch (SQLException e) {
            throw new RuntimeException("查询失败", e);
        }
    }

    @Override
    public Staff selectById4Bean(int id) {
        String sql = "select id,name,phone,age,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs where id=?";
        try {
            return qr.query(sql, new BeanHandler<>(Staff.class), id);
        } catch (SQLException e) {
            throw new RuntimeException("查询异常", e);
        }
    }

    @Override
    public List<Staff> selectAll4BeanList() {
        String sql = "select id,name,phone,age,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs";
        try {
            return qr.query(sql, new BeanListHandler<>(Staff.class));
        } catch (SQLException e) {
            throw new RuntimeException("查询异常", e);
        }
    }

    @Override
    public String selectById4ColumnName(int id) {
        String sql = "select name from staffs where id=?";
        try {
            return qr.query(sql, new ScalarHandler<>(), id);
        } catch (SQLException e) {
            throw new RuntimeException("查询异常", e);
        }
    }

    @Override
    public Long selectByCount() {
        String sql = "select count(id) from staffs";
        try {
            return qr.query(sql, new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException("查询异常", e);
        }
    }

    @Override
    public List<String> selectAllName() {
        String sql = "select name from staffs";
        try {
            return qr.query(sql, new ColumnListHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException("查询异常", e);
        }
    }

    @Override
    public Object[] selectById4Array(int id) {
        String sql = "select id,name,phone,age,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs where id=?";
        try {
            return qr.query(sql, new ArrayHandler(),id);
        } catch (SQLException e) {
            throw new RuntimeException("查询异常", e);
        }
    }

    @Override
    public List<Object[]> selectAll4ArrayList() {
        String sql = "select id,name,phone,age,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs";
        try {
            return qr.query(sql, new ArrayListHandler());
        } catch (SQLException e) {
            throw new RuntimeException("查询异常", e);
        }
    }
}
